/**
 * @author Administrator
 * 多选择人员树
 */
var selectusercheckwin,selectusercheckididtager='',userchecksm,checkoldnode=0, 
selectuserchecknametager='',selectuserchecknodetager = '',selectcheckusersort='',
checkuserid=new Ext.data.Store();
function initselectusercheck(){
	
	 Ext.BLANK_IMAGE_URL=Ext.get('basePath').getValue()+"ajaxFramework/ext-2.1/resources/images/default/s.gif";
	 var url = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/departmenttree.do';
	 var load = new Ext.tree.TreeLoader({
	 	dataUrl:url
	 });
	 var root = new Ext.tree.AsyncTreeNode({
	 	id:'0',
	 	text:'天津利达粮油有限公司'
	 })
	 var tree = new Ext.tree.TreePanel({
	 		x:0,
	 		y:0,	
        	autoScroll: true,
        	animate: true,
        	containerScroll: true,
        	border: true,
	        loader:load,	 
	        height:420,
	        width:230,
	        anchor:'0% 100%'
	    });
	tree.setRootNode(root); 
	tree.on('click',function(node){
		selectusercheckarray()
		selectcheckusersort.load({
			params:{departmentid:node.id},
			callback:function(r,options,success){
				for(var i=0;i<r.length;i++)
				{
					var index = checkuserid.findBy(function(record,id){
						if(record.data.nodeid == node.id && record.data.userId ==  r[i].data.userId)
						{
							userchecksm.selectRecords([r[i]],true);
						}
					});
				}
			} 
		});
		checkoldnode=node.id;
	})
	/**
	 * 用户列表
	 */
	var userlisturl = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/selectUserByDepartmentList.do'
	selectcheckusersort = new Ext.data.JsonStore({
		url:userlisturl,
		fields:['userId','uuid','userName']
	});
	userchecksm  = new Ext.grid.CheckboxSelectionModel({
		handleMouseDown:Ext.emptyFn
	});
	var cm = new Ext.grid.ColumnModel([
		userchecksm,
		{header: "用户名", width: 75, dataIndex: 'userName',menuDisabled : true},
		{header: "userId", width: 75, dataIndex: 'userId',menuDisabled : true,hidden:true},
		{header: "uuid", width: 75, dataIndex: 'uuid',menuDisabled : true,hidden:true}
	]);
	var grid = new Ext.grid.GridPanel({
		x:tree.width,
	 	y:0,
		cm:cm,
		sm:userchecksm,
		width:270,
		height:420,
		anchor:'100% 100%',
		store:selectcheckusersort,
		border: true,
	   	viewConfig:{
			forceFit:true	
		}
	});
	selectusercheckwin = new Ext.Window({
		layout:'absolute',
		title:'人员选择',
		width:520,
		height:500,
		modal:true,
		closeAction:'hide',
		items:[tree,grid],
		buttonAlign:'right',
		buttons:[{
			text:'保存',
			handler:function(){
				var userid="",username="",nodeid="";
				selectusercheckarray();
				checkuserid.each(function(record){
					
				
					if(typeof(record.data.userName) ==  'undefined')
					{
						
					}
					else
					{
						userid+=record.data.userId+",";
						username+=record.data.userName+",";
						nodeid+=record.data.nodeid+",";	
					}
					
					
				})
				username=username.substring(0,username.length-1);
				$('#'+selectusercheckididtager).attr('value',userid);			
				$('#'+selectuserchecknametager).attr('value',username);
				$('#'+selectuserchecknodetager).attr('value',nodeid);
				selectusercheckwin.hide();
			}
		},{
			text:'取消',
			handler:function(){
				selectusercheckwin.hide();		
			}
		}]
		
	})
}
/**
 * 打开人员的多选树
 * @param {} id
 * @param {} name
 */
function selectusercheckwinshow(id,name,nodeid){
	selectusercheckididtager = id;
	selectuserchecknametager = name;
	selectuserchecknodetager = nodeid;
	userchecksm,checkoldnode=0,selectcheckusersort.removeAll();
	var userid = $('#'+selectusercheckididtager).attr('value');
	var username = $('#'+selectuserchecknametager).attr('value');
	var nodeid = $('#'+selectuserchecknodetager).attr('value');
	if(typeof(userid)   ==   'undefined')
	{
		checkuserid.removeAll();
	}
	else
	{
		checkuserid.removeAll();
		var useridarray = userid.split(",");
		var usernamearray = username.split(",");
		var nodeidarray =  '';
		if(typeof(userid)   ==   'undefined')
		{
			nodeidarray = nodeid.split(",");	
		}
		
		for(var i = 0;i<useridarray.length;i++)
		{
			checkuserid.addSorted(new Ext.data.Record({
				nodeid:nodeidarray[i],
				userId:useridarray[i],
				userName:usernamearray[i]
			}));
		}
	}
	selectusercheckwin.show();
	selectusercheckwin.findByType('treepanel')[0].root.expand();  
}
/**
 * 添加数组中的数据
 * @param {} id
 */
function selectusercheckarray(){
	clearusercheckarray();
	var rlolesarray,nouserselectaray,index;
	rlolesarray = userchecksm.getSelections();
	for(var i =0;i<rlolesarray.length;i++)
	{
		index = checkuserid.findBy(function(record,id){
			 return record.data.nodeid == checkoldnode && rlolesarray[i].data.userId == record.data.userId	
		})
		if(checkuserid.findBy(function(record,id){return  rlolesarray[i].data.userId == record.data.userId}) == -1)
		 {
			if(index == -1 ||checkuserid.getCount() ==0)
			{
				checkuserid.addSorted(new Ext.data.Record({
					nodeid:checkoldnode,
					userId:rlolesarray[i].data.userId,
					userName:rlolesarray[i].data.userName
				}))
			}
		 }   
	}	
}
/**
 * 从数组中清除未被选中的数据
 */
function clearusercheckarray(){
	selectcheckusersort.each(function(r){
		if(userchecksm.isSelected(r) == false)
		{
			var index =	checkuserid.findBy(function(record,id){
				return  record.data.nodeid == checkoldnode && record.data.userId == r.data.userId
			})
			checkuserid.remove(checkuserid.getAt(index));
		}
	})
	
}